package com.javasm.controller.stockController;

import com.javasm.common.http.AxiosResult;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.stockCtriteria.OutStorageCriteria;
import com.javasm.domin.entity.stockEntity.OutBriefList;
import com.javasm.domin.entity.stockEntity.OutDetailedOrder;
import com.javasm.domin.entity.stockEntity.OutStorageImel;
import com.javasm.domin.vo.stockVo.OutBriefListVo;
import com.javasm.domin.vo.stockVo.OutDetailedInfoVo;
import com.javasm.domin.vo.stockVo.OutDetailedOrderImelVo;
import com.javasm.domin.vo.stockVo.OutDetailedOrderVo;
import com.javasm.mapper.stockMapper.OutBriefListMapper;
import com.javasm.mapper.stockMapper.OutDetailedOrderMapper;
import com.javasm.service.stockService.ExcelService;
import com.javasm.service.stockService.OutBriefListService;
import com.javasm.service.stockService.OutDetailedOrderService;
import com.javasm.service.stockService.OutStorageImelService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.javasm.common.dictionary.InitContext.dic;



/**
 * 销售出库接口
 */
@RestController
@RequestMapping("/stockOut")
@RequiredArgsConstructor
public class SalesOutboundController {

    //库存简表
    private final OutBriefListService outBriefListService;

    //库存详细
    private final OutDetailedOrderService outDetailedOrderService;

    //出库商品串码表
    private final OutStorageImelService outStorageImelService;

    private final ExcelService excelService;

    private final OutDetailedOrderMapper outDetailedOrderMapper;

    private final OutBriefListMapper outBriefListMapper;


    @RequestMapping("/dic")
    public AxiosResult<Map<Integer, LinkedHashMap<Integer, String>>> data() {
        return AxiosResult.success(dic);
    }


    /**
     * 简要订单列表
     *
     * @param outStorageCriteria
     * @return
     */
    @RequestMapping("briefList")
    public AxiosResult<PageResult<OutBriefListVo>> BriefList(OutStorageCriteria outStorageCriteria) {
        PageResult<OutBriefListVo> list = outBriefListService.selectPage(outStorageCriteria);

        return AxiosResult.success(list);
    }


    /**
     * 出库列表
     *
     * @param id
     * @return
     */
    @RequestMapping("detailedList")
    public List<OutDetailedOrder> DetailedList() {
        return outDetailedOrderService.list();
    }


    /**
     * 查询出库商品列表
     *
     * @param id
     * @return
     */

    @RequestMapping("detailedById/{id}")
    public AxiosResult<List<OutDetailedOrderVo>> DetailedById(@PathVariable Serializable id) {
        List<OutDetailedOrderVo> list = outDetailedOrderService.selectDetailedId(id);
        return AxiosResult.success(list);
    }


    /**
     * 查询订单info
     */
    @RequestMapping("detailedInfoById/{id}")
    public AxiosResult<OutDetailedInfoVo> DetailedInfoById(@PathVariable Serializable id) {
        OutDetailedInfoVo infoVo = outBriefListService.detailedInfoById(id);
        return AxiosResult.success(infoVo);
    }


    /**
     * 销售出库订单已出库查看页面,查询出库商品串号列表
     */
    @RequestMapping("detailedOrderImelList/{id}")
    public AxiosResult<List<OutDetailedOrderImelVo>> DetailedOrderImelList(@PathVariable Serializable id) {
        List<OutDetailedOrderImelVo> orderImelVos = outDetailedOrderService.selectDetailedImelId(id);
        return AxiosResult.success(orderImelVos);
    }



    /**
     * 插入简要订单
     *
     * @param outBriefList
     * @return
     */
    @RequestMapping("insertBriefOrder")
    public int insertBriefOrder(OutBriefList outBriefList) {
        outBriefList.setVipAccount("SB"+ (int) ((Math.random() * 9 + 1) * 100000));
        //添加销售出库订单号
        outBriefList.setSalesOutOrderId("DD"+(int) ((Math.random() * 9 + 1) * 100000));
        outBriefList.setOrderDate(outBriefList.getOrderDate());
        outBriefList.setSalesOutOrderDate(new Date());
        outBriefList.setDetailOrderId(outBriefList.getDetailOrderId());
        outBriefListMapper.insert(outBriefList);
        return 1;
    }

    /**
     * 插入详细订单
     *
     * @param outDetailedOrder
     * @return
     */
    @RequestMapping("insertDetailedOrder")
    public Boolean insertDetailedOrder(OutDetailedOrder outDetailedOrder) {
        outDetailedOrder.setOrderId(outDetailedOrder.getOrderId());
        int detailImelId = outDetailedOrderMapper.lastDetailedOrder().getImelId() + 1;
        outDetailedOrder.setImelId(detailImelId);
        return outDetailedOrderService.save(outDetailedOrder);
    }


    /**
     * 插入单条串码
     */
    @PostMapping("insertOrderImel")
    public Boolean insertOrderImel(OutStorageImel outStorageImel) {
        return outStorageImelService.save(outStorageImel);
    }



//    @RequestMapping("/exportExcel")
//    public void exportExcel(HttpServletResponse response) throws Exception {
//        String fileName = "销售出库单";
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("utf-8");
//        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//        fileName = URLEncoder.encode(fileName, "UTF-8");
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
//        List<OutDetailedOrderVo> orderVos = outDetailedOrderService.selectDetailedId(1);
//        List<OutDetailedOrderImelVo> imelVos = outDetailedOrderService.selectDetailedImelId(1);
//        List<OutStorageImel> detailedOrderImel = outStorageImelService.findDetailedOrderImel(1);
//        WriteSheet writeSheet = EasyExcel.writerSheet(0, "出库商品列表").head(OutDetailedOrderVo.class).build();
//        excelWriter.write(orderVos, writeSheet);
//        writeSheet = EasyExcel.writerSheet(1, "出库商品串号列表").head(OutStorageImel.class).build();
//        excelWriter.write(detailedOrderImel, writeSheet);
//        excelWriter.finish();
//    }


    /**
     * 创建Excel
     * @param response
     * @param id
     */
    @RequestMapping(value = "create/{id}", produces = "application/vnd.ms-excel;charset=UTF-8")
    public void createExcel(HttpServletResponse response, @PathVariable Integer id) {
        excelService.createExcel(response, id);
    }

    @RequestMapping(value = "createBatchExcel/{ids}", produces = "application/vnd.ms-excel;charset=UTF-8")
    public void batchExcel(HttpServletResponse response, @PathVariable List<String> ids) {
        excelService.createBatchExcel(response, ids);
    }




    /**插入串码
     *
     * @param ids
     * @return
     */

    @RequestMapping("insertImel/{ids}")
    public AxiosResult<String> insertImel(@PathVariable List<String> ids) {
        outDetailedOrderService.insertImel(ids);
        return AxiosResult.success("插入成功");
    }

    @RequestMapping("holdImel/{ids}")
    public AxiosResult<String> HoldImel(@PathVariable List<String> ids) {
        outDetailedOrderService.holdImel(ids);
        return AxiosResult.success("暂存成功");
    }



}
